#include "messageshowarea.h"
#include<QScrollBar>
#include <QTimer>
#include<QVBoxLayout>
#include"messageitem.h"
MessageShowArea::MessageShowArea(QWidget *parent)
    : QScrollArea(parent)
{
    //创建我们的垂直布局管理器
    QVBoxLayout*layout=new QVBoxLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0,0,0,0);
    //创建滚动区域核心空间
    container=new QWidget();
    container->setLayout(layout);
    //设置展示区的属性和样式
    this->setMinimumHeight(200);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    this->setObjectName("messageshowarea");
    this->setStyleSheet("#messageshowarea{background:transparent;border:none;border-bottom:1px solid gray;}");
    this->setWidgetResizable(true);
    this->setWidget(container);
    //创建我们的滚动条
    this->verticalScrollBar()->setStyleSheet(
        "QScrollBar:vertical {"
        "   width: 8px;"
        "   background: rgba(255, 255, 255, 0.05);"
        "   border-radius: 4px;"
        "   margin: 0px;"
        "}"
        "QScrollBar::handle:vertical {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff6b6b, stop:1 #06d6a0);"
        "   border-radius: 4px;"
        "   min-height: 30px;"
        "}"
        "QScrollBar::handle:vertical:hover {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff8e8e, stop:1 #08e6b0);"
        "}"
        "QScrollBar::handle:vertical:pressed {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff4b4b, stop:1 #04c690);"
        "}"
        "QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {"
        "   border: none;"
        "   background: none;"
        "   height: 0px;"
        "}"
        "QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {"
        "   background: none;"
        "}");
    QString horizontalStyle="QScrollBar:horizontal {height: 0px;}";
    this->horizontalScrollBar()->setStyleSheet(horizontalStyle);



}

void MessageShowArea::addMessageItem(bool isleft, const Message &messageinfor)
{
    MessageItem*item=MessageItem::makeMessageItem(isleft,messageinfor);
    QVBoxLayout*layout= dynamic_cast<QVBoxLayout*>(container->layout());
    if(isleft)
    {
        layout->addWidget(item);
    }
    else
    {
        layout->addWidget(item);
    }

}

void MessageShowArea::clear()
{

    QVBoxLayout*layout= dynamic_cast<QVBoxLayout*>(container->layout());
    for(int i=layout->count()-1;i>=0;i--)
    {
        QLayoutItem *tem=layout->takeAt(i);
        if(tem==nullptr||tem->widget()==nullptr) continue;
        delete tem->widget();
        tem=nullptr;
    }
}
